export default function usePath(){
    //method
    const isExternal = (path: string)=> {
        return /^(https?:|http?:|mailto:|tel:)/.test(path)
    }
    const router = useRouter()
    const pushPath = (path:string,query?:any)=>{
        if(isExternal(path)){
            window.open(path,'_blank')
        }else{
            if(!query)
                router.push(path).catch(err => {
                    console.log(err);
                });
            else
                router.push({path:path,query:query}).catch(err => {
                    console.log(err);
                });
        }
    }
    const replacePath = (path:string,query?:any)=>{
        if(!query)
            router.replace(path).catch(err => {
                console.log(err);
            });
        else
            router.replace({path:path,query:query}).catch(err => {
                console.log(err);
            });
    }
    const goBack=()=>{
        router.back()
    }
    return {
        pushPath,replacePath,goBack,isExternal
    }
}